package com.sport.facilities.booking.system.httptask;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;

import com.sport.facilities.booking.system.core.HttpHelper;
import com.sport.facilities.booking.system.model.Booking;
import com.sport.facilities.booking.system.model.BookingRequestModel;
import com.sport.facilities.booking.system.model.MakeBookFormModel;
import com.sport.facilities.booking.system.model.UserInfo;

public class BasicTask
		extends
		AsyncTask<Object/* url */, Integer /* Progress */, String /* Result */> {
	private final String TAG = "BasicTask";
	private Context ctx = null;
	protected UserInfo uInfo = null;

	public static final String PATH_LOGIN_PAGE = "/saosport/";
	public static final String PATH_LOGIN = "/saosport/login";
	public static final String PATH_MAIN_MENU = "/saosport/spmenu";
	public static final String PATH_INQ_BOOK = "/saosport/spinquire";
	public static final String PATH_CONFIRM_BOOK = "/saosport/spclaim";
	public static final String PATH_INQ_CONFIRM = "/saosport/spclaimed";
	public static final String PATH_NOTICE = "/saosport/spnotice";
	public static final String PATH_BOOK_PAGE = "/saosport/spbook";
	public static final String PATH_BOOK_CHECK = "/saosport/spmakebk";
	public static final String PATH_BOOK_SUBMIT = "/saosport/spbkhandler";
	public static final String PATH_BOOK_IMG = "/saosport/Kaptcha.jpg";
	public static final String PATH_CANCEL_BOOK = "/saosport/spcancel";
	public static final String PATH_POST_CANCEL_BOOK = "/saosport/spcanhandler";
	public static final String PATH_POST_CONFIRM_BOOK = "/saosport/spclaimhandler";
	public static final String PATH_TRANSFER_BOOK = "/saosport/sptransfer";
	public static final String PATH_POST_TRANSFER_BOOK = "/saosport/sptransbk";
	public static final String PATH_POST_TRANSFER_FORM = "/saosport/sptranhandler";

	private ProgressDialog pdialog;

	protected OnTaskCompleted listener;

	public BasicTask(Context ctx) {
		this.ctx = ctx;
	}

	public BasicTask(Context ctx, UserInfo uInfo) {
		this(ctx);
		this.uInfo = uInfo;
	}

	private UserInfo getUserInfo() {
		if (this.uInfo == null) {
			this.uInfo = new UserInfo();
		}
		return uInfo;
	}

	/*
	 * Http Client Establish Connection
	 */
	@Override
	protected String doInBackground(Object... urls) {
		Object url = urls[0];
		String result = null;
		try {
			if (url == PATH_LOGIN_PAGE) {
				result = fnLoadLogin();
			} else if (url == PATH_LOGIN) {
				result = fnLogin();
			} else if (url == PATH_NOTICE) {
				result = fnGetNotice();
			} else if (url == PATH_INQ_BOOK) {
				result = fnGetInqBook();
			} else if (url == PATH_INQ_CONFIRM) {
				result = fnGetInqConfirm();
			} else if (url == PATH_CONFIRM_BOOK) {
				result = fnGetConfirmBookPage();
			} else if (url == PATH_BOOK_PAGE) {
				result = fnGetBookPage();
			} else if (url == PATH_BOOK_CHECK) {
				MakeBookFormModel bm = (MakeBookFormModel) urls[1];
				result = fnCheckBookPage(bm);
			} else if (url == PATH_BOOK_SUBMIT) {
				Booking bm = (Booking) urls[1];
				MakeBookFormModel bfm = (MakeBookFormModel) urls[2];
				result = fnSubmitBooking(bm,bfm);
			} else if (url == PATH_CANCEL_BOOK) {
				result = fnGetCancelBookPage();
			} else if (url == PATH_POST_CANCEL_BOOK) {
				BookingRequestModel bm = (BookingRequestModel) urls[1];
				result = fnPostCancelBookPage(bm);
			} else if (url == PATH_POST_CONFIRM_BOOK) {
				BookingRequestModel bm = (BookingRequestModel) urls[1];
				result = fnPostConfirmBookPage(bm);
			} else if (url == PATH_TRANSFER_BOOK) {
				result = fnGetTransferBookPage();
			} else if (url == PATH_POST_TRANSFER_BOOK) {
				BookingRequestModel bm = (BookingRequestModel) urls[1];
				result = fnPostTransferBookPage(bm);
			} else if (url == PATH_POST_TRANSFER_FORM) {
				BookingRequestModel bm = (BookingRequestModel) urls[1];
				result = fnPostTransferBookForm(bm);
			}

		} catch (Exception e) {
			if (listener != null) {
				listener.onTaskFailed(e.getMessage());
			}
			Log.e(TAG, e.getMessage());
		}
		return result;
	}

	private String fnLoadLogin() throws Exception {
		Log.d(TAG, "fnLoadLogin()");
		HttpHelper httpHelper = new HttpHelper();
		return httpHelper.doGet(PATH_LOGIN_PAGE);
	}

	private String fnLogin() throws Exception {
		/*
		 * <form action="/saosport/login" method="post">
		 * 
		 * (1)Student/Staff <input type="radio" name="userType" value="1"
		 * checked="" onclick="switchCurrent();">
		 * 
		 * (2)Others <input type="radio" name="userType" value="2"
		 * onclick="switchOther();">
		 * 
		 * (3)Net_Id <input type="text" name="name" maxlength="9">
		 * 
		 * (4)Password <input name="passwd" type="password" maxlength="20">
		 */
		Log.d(TAG, "fnLogin()");
		// nameValuePairs
		List<NameValuePair> nvps = new ArrayList<NameValuePair>();
		nvps.add(new BasicNameValuePair("userType", getUserInfo().getUserType()
				+ ""));
		nvps.add(new BasicNameValuePair("name", getUserInfo().getName()));
		nvps.add(new BasicNameValuePair("passwd", getUserInfo().getPass()));

		HttpHelper httpHelper = new HttpHelper();
		return httpHelper.doPost(PATH_LOGIN, nvps);
	}

	private String fnGetNotice() throws Exception {
		Log.d(TAG, "fnGetNotice()");
		HttpHelper httpHelper = new HttpHelper();
		return httpHelper.doPost(PATH_NOTICE, null);
	}

	private String fnGetInqBook() throws Exception {
		Log.d(TAG, "fnGetInqBook()");
		HttpHelper httpHelper = new HttpHelper();
		return httpHelper.doPost(PATH_INQ_BOOK, null);
	}

	private String fnGetInqConfirm() throws Exception {
		Log.d(TAG, "fnGetInqConfirm()");
		HttpHelper httpHelper = new HttpHelper();
		return httpHelper.doPost(PATH_INQ_CONFIRM, null);
	}

	private String fnGetBookPage() throws Exception {
		Log.d(TAG, "fnGetBookPage()");
		HttpHelper httpHelper = new HttpHelper();
		return httpHelper.doPost(PATH_BOOK_PAGE, null);
	}

	private String fnCheckBookPage(MakeBookFormModel bm) throws Exception {

		List<NameValuePair> nvps = new ArrayList<NameValuePair>();
		nvps.add(new BasicNameValuePair(bm.getActivitySubmitName(), bm.getActivity()));
		nvps.add(new BasicNameValuePair(bm.getSubmitName(), bm.getSubmitValue()));
		nvps.add(new BasicNameValuePair(bm.getSt_daySubmitName(), bm.getSt_day()));
		nvps.add(new BasicNameValuePair(bm.getEd_daySubmitName(), bm.getEd_day()));
		nvps.add(new BasicNameValuePair(bm.getSt_timeSubmitName(), bm.getSt_time()));
		nvps.add(new BasicNameValuePair(bm.getEd_timeSubmitName(), bm.getEd_time()));
		nvps.add(new BasicNameValuePair(bm.getCentreSubmitName(), bm.getCentre()));
		nvps.add(new BasicNameValuePair(bm.getKaptchaName(), bm.getKaptchaValue()));
		Log.d(TAG, "fnCheckBookPage()" + nvps.toString());
		HttpHelper httpHelper = new HttpHelper();
		return httpHelper.doPost(PATH_BOOK_CHECK, nvps);
	}
	
	private String fnSubmitBooking(Booking bm, MakeBookFormModel bfm) throws Exception {
		List<NameValuePair> nvps = new ArrayList<NameValuePair>();
		
		nvps.add(new BasicNameValuePair(bfm.getActivityCodeName(), bfm.getActivityCodeValue()));
		nvps.add(new BasicNameValuePair("radiobutton", bm.getId()));
		nvps.add(new BasicNameValuePair(bfm.getKaptchaName(), bfm.getKaptchaValue()));
		Log.d(TAG, "fnSubmitBooking()" + nvps.toString());
		HttpHelper httpHelper = new HttpHelper();
		return httpHelper.doPost(PATH_BOOK_SUBMIT, nvps);
	}
	
	private String fnGetCancelBookPage() throws Exception {
		Log.d(TAG, "fnGetCancelBookPage()");
		HttpHelper httpHelper = new HttpHelper();
		return httpHelper.doPost(PATH_CANCEL_BOOK, null);
	}

	private String fnGetConfirmBookPage() throws Exception {
		Log.d(TAG, "fnGetConfirmBookPage()");
		HttpHelper httpHelper = new HttpHelper();
		return httpHelper.doPost(PATH_CONFIRM_BOOK, null);
	}

	private String fnGetTransferBookPage() throws Exception {
		Log.d(TAG, "fnGetTransferBookPage()");
		HttpHelper httpHelper = new HttpHelper();
		return httpHelper.doPost(PATH_TRANSFER_BOOK, null);
	}

	private String fnPostCancelBookPage(BookingRequestModel bm) throws Exception {
		/*
		 * <FORM name=mainform method=post> <input type='radio'
		 * name='radiobutton' value=57798042> </FORM>
		 */
		Log.d(TAG, "fnPostCancelBookPage()" + bm.getId());

		// nameValuePairs
		List<NameValuePair> nvps = new ArrayList<NameValuePair>();
		nvps.add(new BasicNameValuePair("radiobutton", bm.getId()));

		HttpHelper httpHelper = new HttpHelper();
		return httpHelper.doPost(PATH_POST_CANCEL_BOOK, nvps);
	}

	private String fnPostConfirmBookPage(BookingRequestModel bm) throws Exception {
		/*
		 * <FORM name=mainform method=post> <input type='radio'
		 * name='radiobutton' value=57798042> </FORM>
		 */
		Log.d(TAG, "fnPostConfirmBookPage()" + bm.getId());

		// nameValuePairs
		List<NameValuePair> nvps = new ArrayList<NameValuePair>();
		nvps.add(new BasicNameValuePair("radiobutton", bm.getId()));

		HttpHelper httpHelper = new HttpHelper();
		return httpHelper.doPost(PATH_POST_CONFIRM_BOOK, nvps);
	}

	private String fnPostTransferBookPage(BookingRequestModel bm) throws Exception {
		/*
		 * <FORM name=mainform method=post> <input type='radio'
		 * name='radiobutton' value=57798042> </FORM>
		 */
		Log.d(TAG, "fnPostTransferBookPage()" + bm.getId());

		// nameValuePairs
		List<NameValuePair> nvps = new ArrayList<NameValuePair>();
		nvps.add(new BasicNameValuePair("radiobutton", bm.getId()));

		HttpHelper httpHelper = new HttpHelper();
		return httpHelper.doPost(PATH_POST_TRANSFER_BOOK, nvps);
	}

	private String fnPostTransferBookForm(BookingRequestModel bm) throws Exception {
		/*
		 <input type= hidden name='pkey' value =57937850>
		 <input type='text' name='ltouid'>
		 <input type='text' name='uname'>
		 */
		Log.d(TAG, "fnPostTransferBookForm()" + bm.getId());

		// nameValuePairs
		List<NameValuePair> nvps = new ArrayList<NameValuePair>();
		nvps.add(new BasicNameValuePair("pkey", bm.getId()));
		nvps.add(new BasicNameValuePair("ltouid", bm.getTransferId()));
		nvps.add(new BasicNameValuePair("uname", bm.getTransferSurname()));

		HttpHelper httpHelper = new HttpHelper();
		return httpHelper.doPost(PATH_POST_TRANSFER_FORM, nvps);
	}

	@Override
	protected void onCancelled() {
		super.onCancelled();
	}

	@Override
	protected void onPreExecute() {
		if (pdialog == null) {
			pdialog = new ProgressDialog(ctx);
			// pdialog.setButton("cancel", new DialogInterface.OnClickListener()
			// {
			// public void onClick(DialogInterface dialog, int i) {
			// dialog.cancel();
			// }
			// });
			// pdialog.setOnCancelListener(new
			// DialogInterface.OnCancelListener() {
			// public void onCancel(DialogInterface dialog) {
			// pdialog.cancel();
			// }
			// });
			pdialog.setCancelable(true);
			pdialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
			pdialog.setMessage("Loading data from server...");
		}
		pdialog.show();
	}

	@Override
	protected void onPostExecute(String result) {
		fnVoidCloseDialog();
	}

	@Override
	protected void onProgressUpdate(Integer... values) {
	}

	protected void fnVoidCloseDialog() {
		if (pdialog != null) {
			pdialog.cancel();
		}
	}
}
